$(document).ready(function () {

    var user = $("#user").val();

    console.log("user ---- : " + user);

    //查看预约
    $('.show-reservation').click(function () {
        var id = $(this).attr('role');
        $(location).attr('href', '/reservation-add?id=' + id + '&update=' + 1);
        // $(location).attr('href', '/reservation-show?id=' + id + "&update=" + 0);
    });

    // $('.item-tr').click(function () {
    //     var id = $(this).find('.show-reservation').attr('role');
    //     // var id = $('.show-reservation').attr('role');
    //     $(location).attr('href', '/reservation-show?id=' + id + "&update=" + 0);
    // });

    //修改预约
    $('.update-reservation').click(function () {
        var id = $(this).attr('role');
        //转到更新页面
        $(location).attr('href', '/reservation-add?id=' + id + '&user=' + user + '&update=' + 2);
        // $(location).attr('href', '/reservation-show?id=' + id + "&update=" + 1);
    });

    //删除预约
    $('.remove-reservation').click(function () {

        var id = $(this).attr('role');
        var dataStatus = $(this).attr('status');

        var data = {'id': id, 'user': user};

        console.log('dataStatus  :   ' + dataStatus);
        if (dataStatus == 'finished' || dataStatus == 'failed') { //如果是已完成或者申请失败， 直接删除
            removeReservation(data);
        } else {
            bootbox.confirm({
                message: '确定删除该预约吗?',
                buttons: {
                    confirm: {
                        label: '删除',
                        className: 'btn-success'
                    },
                    cancel: {
                        label: '取消',
                        className: 'btn-danger'
                    }
                },
                callback: function (result) {
                    if (result)
                        removeReservation(data);
                }
            });
        }

    });


    //删除预约
    function removeReservation(data) {
        $.post('/reservation-remove', data, function (result) {
            console.log('result id : ' + result.id);
            if (result.id == 1) { //删除成功
                $(location).attr('href', '/reservation-my?user=' + user);
                generateNotify('', result.msg, 'success');
            } else {
                generateNotify('', result.msg, 'danger');
            }
        });
    }

    $('.star').on('click', function () {
        $(this).toggleClass('star-checked');
    });

    $('.ckbox label').on('click', function () {
        $(this).parents('tr').toggleClass('selected');
    });

    $('.btn-filter').on('click', function () {
        var $target = $(this).data('target');
        if ($target != 'all') {
            $('.table tr').css('display', 'none');
            $('.table tr[data-status="' + $target + '"]').fadeIn('slow');
        } else {
            $('.table tr').css('display', 'none').fadeIn('slow');
        }
    });

});